package cn.edu.njpi.wordcount;

import cn.edu.njpi.utils.WordCountUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.wordcount
 * @date 2021/12/11 下午11:35
 */
public class WordCountDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // 1 获取配置信息以及获取job对象
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop101:9000");
        System.setProperty("HADOOP_USER_NAME", "root");
        Job job = Job.getInstance(conf);

        // 2 关联Driver程序的jar
        job.setJarByClass(WordCountDriver.class);

        // 3 关联Mapprt和Reducer的jar
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCounReducer.class);

        // 4 设置Mapper输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 5 设置最终输出kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 6 设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path("/input"));
        FileOutputFormat.setOutputPath(job, new Path("/output"));

        // 7 提交job
        WordCountUtils.result = job.waitForCompletion(true);
    }
}
